#include <iostream>
#include <string>

#include "OTKXString.h"
#include "OTKXString.cpp"
#include "logger.cpp"
#include <stdio.h>
#include <direct.h>
#include <fstream>
#include <vector>
#include <io.h>

using namespace std;
void GenDatFile(const wchar_t *path, ofstream *file);
int main()
{
    ofstream file;
    file.open("creotk.dat", ios::out);
    char *path;
    if ((path = getcwd(NULL, 0)) == NULL)
        return 0;
    else
    {
        char c1 = 0xEF; // 仿utf-8 BOM头  三字节
        char c2 = 0xBB;
        char c3 = 0xBF;
        file << c1 << c2 << c3;

        wstring wpath;
        gconvert::utf82uni(path, wpath);
        GenDatFile(wpath.c_str(), &file);
    }
    return 0;
}

void GenDatFile(const wchar_t *path, ofstream *file)
{
    //文件句柄
    intptr_t hFile = 0;
    //文件信息
    struct _wfinddata64i32_t fileinfo; //包含中文目录，使用宽字符
    wstring p;
    if ((hFile = _wfindfirst(p.assign(path).append(L"\\*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            if (fileinfo.attrib & _A_SUBDIR)
            {
                if (wcscmp(fileinfo.name, L".") != 0 && wcscmp(fileinfo.name, L"..") != 0)
                {
                    GenDatFile(p.assign(path).append(L"\\").append(fileinfo.name).c_str(), file);
                }
            }
            else
            {
                wstring filename(fileinfo.name);
                string fname;
                gconvert::uni2utf8(filename, fname);
                string fpath;
                gconvert::uni2utf8(wstring(path), fpath);
                if (strcmp(fname.substr(fname.length() - 4).c_str(), ".dll") == 0)
                {
                    *file << "NAME       " << fname.substr(0, fname.length() - 4) << "\n"
                          << "EXEC_FILE  " << fpath << "\\" << fname << "\n"
                          << "TEXT_DIR   " << fpath << "\\text\n"
                          << "STARTUP    dll\n"
                          << "ALLOW_STOP TRUE\n"
                          << "END\n\n";
                }
            }
        } while (_wfindnext(hFile, &fileinfo) == 0);
    }
    _findclose(hFile);
}